//
//  ViewController.m
//  UIHomeWork08-通讯录
//
//  Created by tusm on 16/7/25.
//  Copyright © 2016年 tusm. All rights reserved.
//

#import "ViewController.h"
#import "DetailViewController.h"
#import "AddViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,passValue>
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)NSMutableArray * stuArray;
@property(nonatomic,strong)NSMutableArray * nameArray;
@property(nonatomic,strong)NSIndexPath * index;
@property(nonatomic,strong)NSMutableDictionary * studentDic;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.title = @"通讯录";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(addBarButton:)];
    NSString * path = [[NSBundle mainBundle]pathForResource:@"DLS160710" ofType:@"plist"];
    _stuArray = [NSMutableArray arrayWithContentsOfFile:path];
    //    NSLog(@"%@",_stuArray);
    
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    _tableView.rowHeight = 100;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    //1.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    

    // Do any additional setup after loading the view, typically from a nib.
}
//2.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];

}
//4.
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    return UITableViewCellEditingStyleDelete;
//}
//5.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    [_stuArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"count = %ld",_stuArray.count);
    return _stuArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //    _nameArray = [NSMutableArray array];
    //    for (NSDictionary * dicTemp in _stuArray) {
    //       _nameArray = [dicTemp objectForKey:@"name"];
    //        NSLog(@"%@",_nameArray);
    //    }
    
    //    cell.textLabel.text = _nameArray[indexPath.row];
    cell.textLabel.text = [_stuArray[indexPath.row] objectForKey:@"name"];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController * detailVC= [[DetailViewController alloc]init];
    detailVC.name = [_stuArray[indexPath.row]objectForKey:@"name"];
    detailVC.age = [_stuArray[indexPath.row]objectForKey:@"age"];
    detailVC.gender = [_stuArray[indexPath.row]objectForKey:@"gender"];
    detailVC.tele = [_stuArray[indexPath.row]objectForKey:@"phoneNumber"];
    self.index = indexPath;
    [self.navigationController pushViewController:detailVC animated:YES];
}
-(void)addBarButton:(UIButton *)button{
    AddViewController * addVC= [[AddViewController alloc]init];
    addVC.delegate = self;
    [self.navigationController pushViewController:addVC animated:YES];
}

-(void)PassValue:(NSDictionary *)dic{
    
    [_stuArray addObject:dic];
    [_tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
